// ProjectName: go-learn-demo
// PackageName: reflectybxb
// Description:
// Author: hy.bai
// Date: 2025-08-13, 星期三 21:46

package reflectybxb

import (
	"fmt"
	"reflect"
)

type User struct {
	Name string `json:"name"`
	Age  uint8  `json:"age"`
}

func TestStructReflectSetVal() {
	user := User{
		Name: "hy.bai",
		Age:  18,
	}
	userVal := reflect.ValueOf(&user) // 获取结构体的值, 这里传入的是指针，目的是为了修改
	fmt.Println(userVal)              // 打印: {hy.bai 18}
	val := userVal.Elem()             // val是可以修改的
	fmt.Println("是否可以修改：", val.CanSet())
	nameField := val.FieldByName("Name")
	nameField.SetString("ybxb")
	fmt.Println("修改后的值：", val.Interface())
}

func TestIntSetVal() {
	var num int32 = 24
	v := reflect.ValueOf(&num)
	fmt.Println("num:", num, ", elem Kind: ", v.Elem().Kind())
	if v.Elem().Kind() == reflect.Int32 {
		v.Elem().SetInt(300)
	}
	fmt.Println("set num: ", num)
}
